<html>
<head><meta charset="utf-8"><title>Questions about the rowan crate · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html">Questions about the rowan crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218760207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218760207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218760207">(Dec 03 2020 at 21:56)</a>:</h4>
<p>As I understand it, the <code>rowan</code> crate is developed by the <code>rust-analyzer</code> team, right? I had a few questions regarding it.<br>
Mainly, is there a way I can change the "position" of a token? I've just been doing <code>"some code".into()</code> when passing the <code>text</code> argument of <code>GreenNodeBuilder::token</code>, which doesn't appear to do what I want</p>



<a name="218761796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218761796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218761796">(Dec 03 2020 at 22:09)</a>:</h4>
<p>I don't think <code>GreenNode</code>s  even know about positions. Only <code>SyntaxNode</code>s.</p>



<a name="218762390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218762390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218762390">(Dec 03 2020 at 22:14)</a>:</h4>
<p>And from what I can see you cant arbitrarily set a tokens or nodes position as its based on the entire tree, but I might be wrong.</p>



<a name="218762413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218762413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218762413">(Dec 03 2020 at 22:14)</a>:</h4>
<p>Looking at the output of <code>dbg!(&amp;builder)</code>, I think you're right. I had assumed that it might have been something that was part of <code>SmolStr</code>.</p>



<a name="218762649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218762649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218762649">(Dec 03 2020 at 22:16)</a>:</h4>
<p>Any ideas on how I might be able to go about something similar? I want to be able to display the position of errors in source code, just like rust.</p>



<a name="218762731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218762731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218762731">(Dec 03 2020 at 22:17)</a>:</h4>
<p>Why do you need to change the position for that?</p>



<a name="218762807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218762807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218762807">(Dec 03 2020 at 22:18)</a>:</h4>
<p>you want to insert the error messages into the source?</p>



<a name="218764120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218764120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218764120">(Dec 03 2020 at 22:30)</a>:</h4>
<p>No, what I want is to be able to accurately figure out where a token came from in the source code</p>



<a name="218764472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218764472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218764472">(Dec 03 2020 at 22:33)</a>:</h4>
<p>What you want is <a href="https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxNode.html#method.text_range">https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxNode.html#method.text_range</a> and <a href="https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxToken.html#method.text_range">https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxToken.html#method.text_range</a> then I believe</p>



<a name="218764539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218764539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218764539">(Dec 03 2020 at 22:34)</a>:</h4>
<p>Those TextRanges contain the offset and the size the text of the node spans</p>



<a name="218764902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218764902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218764902">(Dec 03 2020 at 22:37)</a>:</h4>
<p>Right, but only within text that has been turned into some token. If i want to ignore whitespace, then all of the positions of keywords will be distorted</p>



<a name="218765752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218765752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218765752">(Dec 03 2020 at 22:46)</a>:</h4>
<p>Well, SyntaxNodes only know their positions based on the entire text of the tree they belong to. If you want to ignore whitespace and have positions according to the whitespace-less text you would have to reconstruct the tree without the whitespace tokens.</p>



<a name="218765884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Questions%20about%20the%20rowan%20crate/near/218765884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Questions.20about.20the.20rowan.20crate.html#218765884">(Dec 03 2020 at 22:48)</a>:</h4>
<p>Ah alright. Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>